/**
 * TmPackFactoryAction.java
 * Created at 2017-3-14
 * Created by xieshuang
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.rms.basic.model.TmPackFactory;
import com.broadtext.rms.basic.service.ITmPackFactoryService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>
 * ClassName: TmPackFactory
 * </p>
 * <p>
 * Description: 电池pack生产厂控制层
 * </p>
 * <p>
 * Author: xieshuang
 * </p>
 * <p>
 * Date: 2017-3-14
 * </p>
 */
@Controller
@RequestMapping("basic/packFactory")
public class TmPackFactoryAction {
    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(TmPackFactoryAction.class);
    /**
     * 注入service
     */
    @Autowired
    @Qualifier("tmPackFactoryService")
    private ITmPackFactoryService tmPackFactoryService;

    /**
     * <p>
     * Description: 电池pack生产厂分页查询
     * </p>
     * 
     * @param request 请求对象
     * @param response 返回对象
     */
    @RequestMapping(value = "/getPackFactory.mvc")
    @ResponseBody
    public Object getPackFactory(HttpServletRequest request, HttpServletResponse response) {
        //代一个码走查 数据类说明
    	Map<String, Object> dataMap;
        dataMap = new HashMap<String, Object>();
        try {
            PageBounds pageBounds;
            pageBounds = new PageBounds();
            Map<String, Object> map;
            String aoData;
            aoData = request.getParameter("aoData");
            JSONArray array;
            array = JSON.parseArray(aoData);
            map = new HashMap<String, Object>();
            if (array != null) {
                Iterator<?> it;
                it = array.iterator();
                while (it.hasNext()) {
                    JSONObject obj;
                    obj = (JSONObject) it.next();
                    if (obj.get("value") != null && !"".equals(obj.get("value") + "")) {
                        map.put(obj.get("name") + "", obj.get("value"));
                    }
                }
            }
            pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
            pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit() + 1);
            EntityPageBean<TmPackFactory> pageBean = this.tmPackFactoryService.findEntityPageByQuery(map, pageBounds);
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());

        } catch (ServiceException e) {
            logger.error("电池pack生产厂分页查询出错：", e);
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: 获取厂区名列表</p>
     * @param request 请求
     * @param response 响应
     * @throws DaoException 异常
     */
    @RequestMapping(value = "/getPackFactoryNameList.mvc")
    @ResponseBody
    public Object getPackFactoryNameList(HttpServletRequest request, HttpServletResponse response) {
        //代码走一个差 一个不一样的数据定义
    	List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        try {
            list = this.tmPackFactoryService.getPackFactoryNameList();
        } catch (DaoException e) {
            logger.error("获取厂区列表出错:", e);
            e.printStackTrace();
        }
        return list;
    }
    
    /**
     * 
     * <p>
     * Description: 新增或更新厂区
     * </p>
     * 
     * @param t 对象
     * @param request 请求
     * @param response 响应
     */
    @RequestMapping(value = "/saveOrUpdate.mvc")
    @ResponseBody
    public Object saveOrUpdate(TmPackFactory t, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> dataMap;
        dataMap = new HashMap<String, Object>();
        try {
            //当前登录人
            String currentUser;
            currentUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
            dataMap.put("msg", this.tmPackFactoryService.saveOrUpdate(t,currentUser));
        } catch (ServiceException | DaoException e) {
            dataMap.put("msg", "FAILURE");
            logger.error("保存出错：", e);
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>
     * Description: 根据id获取编辑对象
     * </p>
     * 
     * @param id 主键id
     * @return 对象
     */
    @RequestMapping(value = "/getPackFactoryById.mvc")
    @ResponseBody
    public TmPackFactory getPackFactoryById(@RequestParam("pkId") String id){
        TmPackFactory t;
        t = new TmPackFactory();
        try {
            t = this.tmPackFactoryService.getPackFactoryById(id);
        } catch (DaoException e) {
            logger.error("获取编辑对象出错：", e);
            e.printStackTrace();
        }
        return t;
    }
    
    
    /**
     * <p> Description: 删除  </p>
     * @param request 请求
     * @param pkId id
     */
    @RequestMapping(value = "/delete.mvc")
    @ResponseBody
    public Object delete(HttpServletRequest request, String pkId){
        String[] idArray;
        idArray = pkId.split(",");
        String updateUser;
        updateUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
        String flag = "";
        try {
            flag = this.tmPackFactoryService.delete(idArray, updateUser);
        } catch (DaoException e) {
            logger.error("删除出错：", e);
            e.printStackTrace();
        }
        Map<String, Object> dataMap;
        dataMap = new HashMap<String, Object>();
        dataMap.put("msg", flag);
        return dataMap;
    }
}
